package zk;


import org.I0Itec.zkclient.IZkDataListener;
import org.I0Itec.zkclient.ZkClient;
import org.I0Itec.zkclient.exception.ZkMarshallingError;
import org.I0Itec.zkclient.serialize.ZkSerializer;
import org.apache.hadoop.yarn.lib.ZKClient;

import java.nio.charset.StandardCharsets;
import java.sql.Connection;

class zkClients {
    private static ZkClient zkClient;
    private static Connection connection;

    public static void main(String[] args) {


    }

    public zkClients() {
    }

    public ZkClient getConnect(String serverstring){
         zkClient = new ZkClient(serverstring);
         return zkClient;
    }
    public void getConnect(){
        zkClient = new ZkClient("linux121:2181");
    }
    public void subscribeDatachange(String path){
        zkClient.subscribeDataChanges(path, new IZkDataListener() {
            @Override
            public void handleDataChange(String s, Object o) throws Exception {
                zkClients zkClients = new zkClients();
                zkClients.getConnect();
                String mysqlPw = zkClients.getMysql("/MysqlPw");
                connection = DruidUtils.getConnection();
            }

            @Override
            public void handleDataDeleted(String s) throws Exception {

            }
        });
    }
    public String getMysql(String path){
        zkClient.setZkSerializer(new ZkSerializer() {
            @Override
            public byte[] serialize(Object o) throws ZkMarshallingError {
                return o.toString().getBytes(StandardCharsets.UTF_8);
            }

            @Override
            public Object deserialize(byte[] bytes) throws ZkMarshallingError {
                return new String(bytes);
            }
        });
        String o = (String)zkClient.readData(path);
        return o;
    }

}
